(function(angular) {
    "use strict";

    var ctl_module = angular.module("cloapCertificateApp.controller");

    ctl_module.controller("certificate_type.listCtl", function($scope, $modal,
            CertificateType, CheckOrgan) {
        var modal_instance = null;

        $scope.widget_title = "证书类型";
        $scope.authorities = CheckOrgan.query();
        $scope.certificate_types = CertificateType.query();

        $scope.editOrAdd = function(operate, certificate_type_id) {
            var ctl = isAdd(operate) ? "certificate_type.addCtl" : "certificate_type.editCtl";

            modal_instance = $modal.open({
                scope: $scope,
                templateUrl: "/cloap/app/cloapCertificateApp/partials/certificate_type_form.html",
                controller: ctl,
                resolve: {
                    params: function() {
                        return {
                            certificate_type_id: certificate_type_id
                        };
                    }
                }
            });

            function isAdd(operate) {
                return operate === "add";
            }
        };

        $scope.saveCertificateType = function(save_method, data, callback) {
            CertificateType[save_method](data, callback, function(response) {
                alert(response.message || "保存失败");
            });
        };

        $scope.toggleState = function(certificate_type) {
            $scope.saveCertificateType("toggle", certificate_type,
                    function toggleSuccess(response_certificate_type) {
                certificate_type.state = response_certificate_type.state;
            });
        };

        $scope.closeModal = function() {
            modal_instance.close();
        };
    });

    ctl_module.controller("certificate_type.editCtl", function($scope, CertificateType, params) {
        var save_method = "update";

        $scope.certificate_type =
            CertificateType.get({certificate_type_id: params.certificate_type_id});
        $scope.widget_title = "编辑证书类型";

        $scope.save = function() {
            $scope.saveCertificateType(save_method, $scope.certificate_type, saveSuccessCallBack);
        };

        function saveSuccessCallBack(response_certificate_type) {
            $scope.certificate_types.splice(
                _.indexOf($scope.certificate_types, $scope.certificate_type),
                1,
                response_certificate_type
            );
            $scope.closeModal();
        }
    });

    ctl_module.controller("certificate_type.addCtl", function($scope) {
        var save_method = "save";
        $scope.certificate_type = {};
        $scope.widget_title = "添加证书类型";

        $scope.save = function() {
            $scope.saveCertificateType(save_method, $scope.certificate_type, saveSuccessCallBack);
        };

        function saveSuccessCallBack(response_certificate_type) {
            $scope.certificate_types.push(response_certificate_type);
            $scope.closeModal();
        }
    });
}(angular));
